package com.labofjet.message;

import java.text.MessageFormat;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.labofjet.entity.Message;

@Service
public class CacheableDBMessageService {

    @Autowired
    MessageRepository repo;

    ConcurrentHashMap<String, Message> map = new ConcurrentHashMap<String, Message>();

    public MessageFormat getMessage(String code) {
        if(StringUtils.isEmpty(code)){
            return null;
        }
        Message m = map.get(code);
        if(m == null){
            m = repo.findByCode(code);
            if(m != null){
                map.put(code, m);
            } else {
                return null;
            }
        }     
        return new MessageFormat(m.getMessage());
    }

}
